﻿/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;
using System.IO;
using Dou.Utils;
using DouEditor.Const;
using UnityEngine;
using Object = UnityEngine.Object;

namespace DouEditor.Resource
{
    /// <summary>
    /// 打包配置
    /// </summary>
    public class BuildRule : ScriptableObject
    {
        /// <summary>
        /// 打包配置描述
        /// </summary>
        public string desc;

        /// <summary>
        /// 构建场景
        /// </summary>
        public Object sceneObj;
        
        /// <summary>
        /// AB 包生成地址
        /// </summary>
        public string outputPath;

        /// <summary>
        /// 最终使用的 AB 包生成地址
        /// </summary>
        public string realOutputPath => outputPath + "/" + BuildConst.BuildFolderName;

        /// <summary>
        /// 拷贝地址，最终生成好的所有可以直接使用的资源目录
        /// </summary>
        public string copyPath;

        /// <summary>
        /// 对照地址，出增量包时用到
        /// </summary>
        public string contrastPath;

        /// <summary>
        /// 增量包输出地址
        /// </summary>
        public string incrementOutputPath;
        
        /// <summary>
        /// 安装包输出地址
        /// </summary>
        public string packageOutputPath;

        /// <summary>
        /// 主版本号
        /// </summary>
        public string mainVersion;

        /// <summary>
        /// 子版本号
        /// </summary>
        public int subVersion;
        
        /// <summary>
        /// 热更版本号
        /// </summary>
        public int hotFixedVersion;

        /// <summary>
        /// 打包前是否清空生成地址文件夹
        /// </summary>
        public bool isClearFolder;
        
        /// <summary>
        /// 是否减构建的 AB 文件拷贝到 StreamingAssets
        /// </summary>
        public bool copyToStreamingAssets;
        
        /// <summary>
        /// 是否开启 AB 包名加密
        /// </summary>
        public bool isEncryptABName;
        
        /// <summary>
        /// 是否开启 AB 包加密
        /// </summary>
        public bool isEncryptAB;

        /// <summary>
        /// 是否是开发者模式
        /// </summary>
        public bool isDev;

        /// <summary>
        /// 是否生成符号表
        /// </summary>
        public bool isSymbol;
        
        /// <summary>
        /// 是否启用SRDebugger
        /// </summary>
        public bool openDebugger;
        
        /// <summary>
        /// 日志等级
        /// </summary>
        public LogLevel logLevel;
        
        /// <summary>
        /// 打包规则
        /// </summary>
        public List<BuildRuleItem> rules = new List<BuildRuleItem>();
    }

    [Serializable]
    public class BuildRuleItem
    {
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool active = true;
        
        /// <summary>
        /// 打包配置描述
        /// </summary>
        public string buildDesc;
        
        /// <summary>
        /// 打包规则类型
        /// </summary>
        public RulePackerType buildType = RulePackerType.SingleFile;

        /// <summary>
        /// 覆盖 AB 包名字（不覆盖使用打包文件夹名字）
        /// </summary>
        public bool isOverrideBundleName = false;
        
        /// <summary>
        /// 覆盖的名字
        /// </summary>
        public string overrideBundleName;
        
        /// <summary>
        /// 打包目录
        /// </summary>
        public string searchPath;
        
        /// <summary>
        /// 打包文件通配符
        /// t: 开头的文本，采用 AssetDatabase.FindAsset 的方式来查找，如：t:Prefab t:Scene t:Material（表示同时查找预制场景和材质类型）
        /// 非 t: 开头的文本，使用 Directory.GetFiles 的方式来查找，如：
        /// </summary>
        public string searchPattern;
        
        /// <summary>
        /// 文件正则匹配代码，填写后只有通过正则匹配后的文件才会打包
        /// </summary>
        public string searchRegex;
        
        /// <summary>
        /// 文件夹搜索选项
        /// </summary>
        public SearchOption searchOption = SearchOption.AllDirectories;
        
        /// <summary>
        /// 包含文件信息写入规则，可以不写入包含的文件到Manifest中，例如图集
        /// </summary>
        public ManifestWriteType manifestWriteType = ManifestWriteType.Write;
    }
}
